#!/bin/sh
#--------------------------------------------------------------
#   FileName  :   SetNetworkInfo.sh
#   Author    :   ghzhangz
#   Descriptor:   Shell 缓存网络配置
#--------------------------------------------------------------

#@example$>sh SetNetworkInfo.sh  -d eth0 -i 172.17.0.0   -n 255.255.0.0   -g172.17.255.250 -b 127.0.0.0 -p
#------------------------------------------------
#declare variables
ARCH=ARM
#ARCH=LINUX
#use -p to enable debug print
debug=false
#---------------network cache path---------------
dataPath="/data/net/cache"
cacheFilePath="${dataPath}/netInfo.dat"
isAutoFlagPath_Prefix="${dataPath}/auto.f"
busyboxBinaryFilePath="/busybox"
busyboxInstallPath="/data/busybox"
nocheck=true
readShellPath="./ReadNetConfig.sh"
#---------------network cache path---------------
if [ $ARCH == "ARM" ]
then
    awkPath="${busyboxInstallPath}/awk"
    routePath="${busyboxInstallPath}/route"
    trPath="${busyboxInstallPath}/tr"
else
    awkPath="awk"
    routePath="route"
    trPath="tr"
fi
netWorkConfig=("ip" "netmask" "gateway" "broadcast" "device")

#------------------------------------------------
# Enumerate known network card names. If the configured network card name is not in the enumeration list,
# Add -s to the parameter for security check
#@example::./SetNetworkInfo.sh  -d eth2 -i 172.17.0.2   -n 255.255.0.2   -g172.17.255.252 -b 127.0.0.2 -ps
NIC_NameArray=("eth0" "eth1" "wlan0" "lo")

#----------------------default ip config---------------------------------------
defaultNetConfig=("ifconfig eth0 192.168.0.100" "ifconfig eth1 192.168.1.100")
#------------------------------------------------------------------------------
function installBusybox() {
    if [ ! -d "$busyboxInstallPath" ]; then
        mkdir -p $busyboxInstallPath
    fi
    if test -e $awkPath
    then
        if [ $debug == true ]; then
            echo "::$LINENO) $busyboxInstallPath already exists"
        fi
    else
        if [ $debug == true ]; then
            echo -e "::$LINENO)\033[33m  awk file::$awkPath does not exist,busybox will be installed \033[0m"
        fi
        $busyboxBinaryFilePath --install -s $busyboxInstallPath
    fi
}

function setOptions() {
    while getopts ":d:b:g:n:i:p s w a c" opts
    do
        case $opts in
        d) netWorkConfig[4]="${netWorkConfig[4]}=$OPTARG";;
        b) netWorkConfig[3]="${netWorkConfig[3]}=$OPTARG";;
        g) netWorkConfig[2]="${netWorkConfig[2]}=$OPTARG";;
        n) netWorkConfig[1]="${netWorkConfig[1]}=$OPTARG";;
        i) netWorkConfig[0]="${netWorkConfig[0]}=$OPTARG";;
        c) rm -rf $dataPath
           exit 0;;
        w) source ${readShellPath}
           startupDaemonIpAddress
           exit 0;;
        a) source ${readShellPath}
           autoObtainsByDHCP $cacheFilePath $isAutoFlagPath_Prefix
           exit 0;;
        #Whether to enable the NIC name security check
        s) nocheck=false ;;
        p) debug=true ;;
        *) echo -e "::$LINENO) \e[1;33;41m (Err)::Unknow arguments [$opts]=$OPTARG \e[0m"
            exit 5;;
        esac
    done
    # install awk
    installBusybox
    # test awk
    $(type ${awkPath})
    if [ $? -ne 0 ]
    then
        echo -e "::$LINENO) \e[1;33;41m (Err)::The awk Command cannot be execute. \e[0m"
        exit 3
    fi
    if [ ! -n "$(echo ${netWorkConfig[4]} | ${awkPath} -F '=' '{print $2}')" ]; then
        echo -e "::$LINENO) \e[1;33;41m (Err)::device name cannot be null. This can be specified using -d \e[0m"
        exit 2
    fi
    if [ $nocheck != true ]; then
        eq=false
        for nicName in ${NIC_NameArray[*]}
        do
            if [ "$nicName" == "$(echo ${netWorkConfig[4]} | ${awkPath} -F '=' '{print $2}')" ]; then
                eq=true
            fi
        done
        if [ $eq != true  ]; then
            echo -e "::$LINENO) \e[1;33;41m (Err)::wrong argument device name [${netWorkConfig[4]}] \e[0m"
            exit 3
        fi
    else
        :
    fi
}

function printOptions() {
    if [ $debug == true ]; then
        for item in ${netWorkConfig[@]}; do
            key=$(echo ${item} | ${awkPath} -F '=' '{print $1}')
            value=$(echo ${item} | ${awkPath} -F '=' '{print $2}')
            if [ -n "$value" ]; then
                if [ $debug == true ]; then
                    echo -e "::$LINENO) \033\e[32m Successful SET $key = ${value} \033[0m"
                fi
            fi
        done
    fi
}

function app_entry(){
    setOptions $@
    printOptions $@
    source ${readShellPath}
    createFiles
    cacheNetworkInfo "${netWorkConfig[*]}"
}

app_entry $@
